<?php
/****
 * zuoyong:门店
 * name:苑立彬
 * time:2022/7/28
 */

namespace App\Http\Controllers\web;

use App\Events\SystemStoreEvent;
use App\Http\Controllers\Controller;
use App\Http\Requests\SystemStoreRequest;
use App\Http\Serve\Es;
use App\Http\Service\web\SystemStoreService;
use App\Models\StoreOrder;
use App\Models\SystemStore;
use App\Repository\RepositoryInterface\SystemStoreInterface;
use Illuminate\Http\Request;


class SystemStoreController extends Controller
{

    protected $systemStore;

    public function __construct(SystemStoreInterface $systemStore)
    {
        $this->systemStore = $systemStore;
        $this->es = new Es();
    }

    /**
     * es搜索
     * @return \Illuminate\Http\JsonResponse
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */

    public function es()
    {
        $serach = \request()->get('name');

        $data = $this->systemStore->search($serach);

        return parent::success('搜索成功', $data);

    }

    /**
     * 门店商品删除
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function systemDel($id)
    {
        try {
            $data = $this->systemStore->systemStoreDel($id);
            if ($data) {
                return parent::success('删除成功');
            } else {
                return parent::error("删除失败");
            }
        } catch (\Exception $exception) {
            return parent::error($exception->getMessage());
        }
    }

    /**
     * 商品列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function productList()
    {
        $id = \request()->get('id');
        $store_name = \request()->get('store_name');
        try {
            $data = SystemStoreService::productListService($id, $store_name);
            return $data;
        } catch (\Exception $exception) {
            return parent::error($exception->getMessage());
        }

    }

    /**
     *  门店状态修改
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update_status(Request $request,$id)
    {
        $data = [
            'status' => $request->post('status'),
            'status_desc' => $request->post('status_desc'),
        ];
        try {
            $this->systemStore->update_status($id, $data);
            $data = SystemStore::find($id);
            event(new SystemStoreEvent($data));
            return parent::success('审核成功');
        } catch (\Exception $exception) {
            return parent::error($exception->getMessage());
        }
    }

    /**
     * 门店流水
     * @return \Illuminate\Http\JsonResponse
     */
    public function storeFlow()
    {
        try {
            $data = StoreOrder::sendList();
            return parent::success('查询成功', $data);
        } catch (\Exception $exception) {
            return parent::error($exception->getMessage());
        }
    }

    /**
     * 门店添加
     * @param Request $request
     */
    public function storeCreated(SystemStoreRequest $request)
    {
        try {
            $data = $request->validated();
            $data = $this->systemStore->systemStoreCreate($data);
            return parent::success('添加门店成功', $data);
        } catch (\Exception $exception) {

            return parent::error($exception->getMessage(), '');
        }
    }


    /**
     * 门店列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function storeList()
    {
        $name = \request()->get('name');
        try {
            $data = $this->systemStore->systemStoreList(10, $name);
            return parent::success('成功', $data);
        } catch (\Exception $exception) {
            return parent::error($exception->getMessage());
        }
    }


    /**
     * 商品下架
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function systemProductUpdate(Request $request)
    {
        $id = $request->get('id');
        $is_show = $request->get('is_show');
        try {
            $this->systemStore->systemProductUpdate($id, $is_show);
            return parent::success('修改成功');
        } catch (\Exception $exception) {
            return parent::error($exception->getMessage());
        }
    }

    /**
     * 修改数据展示接口
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function storeEdit($id)
    {
        try {
            $data = $this->systemStore->systemIsShow($id);

            return parent::success('ok', $data);
        } catch (\Exception $exception) {
            return parent::error($exception->getMessage());
        }
    }

    /**
     * 数据修改
     * @param $id
     * @param Request $request
     * @throws \Exception
     */
    public function storeUpdate($id, SystemStoreRequest $request)
    {
        try {
            $data = $request->validated();
            $result = $this->systemStore->storeUpdate($id, $data);
            if ($result) {
                return parent::success('修改成功');
            } else {
                return parent::error('修改失败');
            }
        } catch (\Exception $exception) {
            return parent::error($exception->getMessage());
        }

    }

    /**
     * 删除
     */
    public function del($id)
    {
        try {
            $this->systemStore->systemDel($id);
            return parent::success('删除成功');
        } catch (\Exception $exception) {
            return parent::error($exception->getMessage());
        }
    }


}
